/*
 * @Date: 2023-01-22 02:17:37
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-24 02:09:09
 * @FilePath: \campus-repair-platform\server\service\manage\announce.js
 */
/**
 * addAnnounce    发布公告
 * updateAnnounce 修改公告
 * deleteAnnounce 删除公告
 */

const announceStatement = require("../../common/sql_sentences/announcement")

exports.addAnnounce = async (req, res) => {
  try {
    const data = req.body
    // 添加公告
    sqlStr = announceStatement.insert({
      ...data,
      job_number: req.auth.job_number
    })
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("添加公告失败！", 10005)
    return res.successSend("添加公告成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateAnnounce = async (req, res) => {
  try {
    const data = req.body
    // 修改公告信息
    sqlStr = announceStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("修改公告信息失败！", 10005)
    return res.successSend("修改公告信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteAnnounce = async (req, res) => {
  try {
    // 删除公告
    const sqlStr = announceStatement.delete(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("删除公告失败！", 10005)
    return res.successSend("删除公告成功！")
  } catch (error) {
    res.errSend(error)
  }
}
